package com.domcer.ultra.menu.application.loader;

import acl.ILoader;
import com.domcer.ultra.menu.BootStrap;
import com.domcer.ultra.menu.application.cache.MenuCache;
import com.domcer.ultra.menu.domain.MenuDomain;
import com.domcer.ultra.menu.domain.MenuSettingsDomain;
import com.domcer.ultra.menu.domain.MenuSettingsOpenItemDomain;
import common.utils.CollectionUtils;
import common.utils.StringUtils;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hang.wang
 * @Date 2025/1/30 22:41
 */
public class MenuFileLoader implements ILoader {

    /**
     * steps: <br>
     * - 1. load all correct menu <br>
     * - 2. storage cache to {@link MenuCache} <br>
     * - 3. assemble command menu <br>
     * - 4. assemble left/right click menu
     */
    @Override
    public void execute0() {
        List<MenuDomain> menuDomainList = this.loadMenus(BootStrap.PLUGIN);
        MenuCache.setMenuDomainList(menuDomainList);

        Map<String, MenuDomain> commandMenuMap = new HashMap<>();
        Map<String, MenuDomain> leftClickMenuMap = new HashMap<>();
        Map<String, MenuDomain> rightClickMenuMap = new HashMap<>();
        menuDomainList.forEach(menuDomain -> {
            MenuSettingsDomain menuSettings = menuDomain.getMenuSettings();
            List<String> commands = menuSettings.getCommands();
            if (CollectionUtils.isNotEmpty(commands)) {
                commands.forEach(command -> {
                    commandMenuMap.put(command, menuDomain);
                });
            }

            MenuSettingsOpenItemDomain openItem = menuSettings.getOpenItem();
            if (openItem != null) {
                String material = openItem.getMaterial().toUpperCase();
                if (StringUtils.isNotEmpty(material)) {
                    if (openItem.isLeftClick()) {
                        leftClickMenuMap.put(material, menuDomain);
                    }
                    if (openItem.isRightClick()) {
                        rightClickMenuMap.put(material, menuDomain);
                    }
                }
            }
        });
        MenuCache.setCommandMenuDomainMap(commandMenuMap);
        MenuCache.setLeftClickMenuDomainMap(leftClickMenuMap);
        MenuCache.setRightClickMenuDomainMap(rightClickMenuMap);
    }

    /**
     * according custom domain to parser yaml <br>
     * will find all custom-menu.yml which under 'plugin/UltraMenu/menu' folder <br>
     * - 1. check folder is existed <br>
     * - 2. find all ends with '.yml' files <br>
     * - 3. invoke {@link MenuDomain#assemble(File)} (File)} method to parser <br>
     * - 4. storage data to cache
     * @param plugin minecraft plugin (just use this variable get data folder path)
     * @return {@link MenuDomain}
     */
    public List<MenuDomain> loadMenus(JavaPlugin plugin) {
        List<MenuDomain> menus = new ArrayList<>();
        File menuFolder = new File(plugin.getDataFolder(), "menu");

        if (!menuFolder.exists() || !menuFolder.isDirectory()) {
            return menus;
        }

        File[] files = menuFolder.listFiles((dir, name) -> name.endsWith(".yml"));

        if (files == null) {
            return menus;
        }

        for (File file : files) {
            MenuDomain menu = MenuDomain.assemble(file);
            menus.add(menu);
        }

        return menus;
    }
}
